<template>
  <view>
    <view>
        <view class="margin-base bg-white padding-tb-2xl radius-2xl padding-lr-base">
            <view class="margin-bottom-lg">
                <aconutInput v-model="from.mobile" title="手机号" placeholder="请输入手机号" />
            </view>
            <view class="margin-bottom-lg">
                <codeInput v-model="from.code" :mobile="from.mobile" :type="sendCodeType" />
            </view>
            <view class="margin-bottom-lg">
                <passwordInput v-model="from.payword" />
            </view>
             <view class="margin-bottom-lg">
                <passwordInput v-model="from.pass_again" placeholder="请再次输入密码"/>
            </view>
        </view>

        <view class="margin-lr-base" style="margin-top: 50rpx">
            <button @click="submit" class="bg-main round text-lg">提 交</button>
        </view>
    </view>
  </view>
</template>

<script lang='ts' setup>
import { ref } from 'vue';
import { useTask } from '@/utils/message';
import { onLoad, onShow } from "@dcloudio/uni-app";
import { useMemberStore } from "@/store/module/member";
import aconutInput from "@/components/input/acount-input.vue";
import passwordInput from "@/components/input/password-input.vue";
import codeInput from "@/components/input/code-input.vue";

const memberStore = useMemberStore();
const type = ref<"pay" | "login">("login");
const sendCodeType = ref<string>("");

const from = ref<AnyObject>({
  mobile: memberStore.userInfo.mobile,
  code: "",
  payword: "",
  pass_again: "",
});

const submit = () => {
  if (!from.value.mobile) {
    useTask("请输入手机号");
    return;
  }
  if (!from.value.code) {
    useTask("请输入验证码");
    return;
  }
  if (!from.value.payword) {
    useTask("请输入密码");
    return;
  }
  if (!from.value.pass_again) {
    useTask("请再次输入密码")
    return;
  }
  if (from.value.payword !== from.value.pass_again) {
    useTask("两次输入密码不一致");
    return;
  }
  memberStore.resetPsd(type.value, {
    username: type.value == 'pay' ? memberStore.userInfo.username : undefined,
    ...from.value
  });
}



const init = () => {
  if (type.value == 'login') {
    uni.setNavigationBarTitle({ title: "重置登陆密码" });
    sendCodeType.value = "reset";
  }
  if (type.value == 'pay') {
    uni.setNavigationBarTitle({ title: "重置支付密码" });
    sendCodeType.value = "setpayword";
  }
}

onLoad((e: any) => {
  if (e.type) {
    type.value = e.type;
    init();
  }
})
</script>

<style scoped lang='scss'>
</style>

